package cn.finder.datatunnel;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/***
 * 加载配置文件
 * @author whl
 *
 */
public class DTConfig {
	
	private static Logger logger=Logger.getLogger(DTConfig.class);
	
	private static final String CONFIG_FILE="dt";
	
	public static String dt_client_read_protocol="";
	public static String dt_client_listen_port="";
	
	public static String dt_client_send_port="";
	public static String dt_client_readdata_size="";
	public static String dt_client_readdata_mark="";
	public static String dt_client_data_processor="";
	public static String dt_server_senddata_size="";
	
	public static String dt_server_send_protocol="";
	public static String dt_server_send_address="";
	public static String dt_server_listen_port="";
	
	public static String dt_client_readdata_endmark="";
	
	
	public static  boolean  load(){  
		
		boolean isSuccess=true;
		
        Properties p = new Properties();  
        try {  
        	//ResourceBundle rb = ResourceBundle.getBundle(CONFIG_FILE,Locale.getDefault(),DTConfig.class.getClassLoader());
        	
        	//ResourceBundle rb = ResourceBundle.getBundle(CONFIG_FILE);
        	
        	String proFilePath=System.getProperty("user.dir")+"\\dt.properties";
        	InputStream inputStream = new BufferedInputStream(new FileInputStream(proFilePath));  
        	ResourceBundle rb = new PropertyResourceBundle(inputStream);  
            inputStream.close();
        	
        	Enumeration eu = rb.getKeys();
    		while(eu.hasMoreElements()){
    			String key = eu.nextElement().toString().trim();
    			String value = rb.getString(key).trim();
    			p.put(key, value);
    		}
        	
            //InputStream in = DTConfig.class.getResourceAsStream("/"+CONFIG_FILE);//这里有人用new FileInputStream(fileName),不过这种方式找不到配置文件。有人说是在classes下，我调过了，不行。  
            //p.load(in);  
            //in.close();
            
            dt_client_read_protocol=p.getProperty("dt.client.read.protocol", "udp");
            
            dt_client_listen_port=p.getProperty("dt.client.listen.port", "8888");
            dt_client_send_port=p.getProperty("dt.client.send.port", "");
            
            dt_client_readdata_size=p.getProperty("dt.client.readdata.size", "10");
            dt_client_readdata_mark=p.getProperty("dt.client.readdata.mark", "\n");
            dt_client_data_processor=p.getProperty("dt.client.data.processor", "cn.finder.datatunnel.processor.DefaultProcessor");
            dt_server_senddata_size=p.getProperty("dt.server.senddata.size", "10");
            dt_server_send_protocol=p.getProperty("dt.server.send.protocol", "udp");
            
            dt_server_send_address=p.getProperty("dt.server.send.address", "");
            dt_server_listen_port=p.getProperty("dt.server.listen.port", "8889");
            
            dt_client_readdata_endmark=p.getProperty("dt.client.readdata.endmark", "mark");
            
            logger.debug("====dt.client.read.protocol:"+dt_client_read_protocol);
	       	 logger.debug("====dt.client.listen.port:"+dt_client_listen_port);
	       	 logger.debug("====dt.client.send.port:"+dt_client_send_port);
	       	 logger.debug("====dt.client.readdata.size:"+dt_client_readdata_size);
	       	 logger.debug("====dt.client.readdata.mark:"+dt_client_readdata_mark);
	       	 logger.debug("====dt.client.data.processor:"+dt_client_data_processor);
	       	 logger.debug("====dt.server.senddata.size:"+dt_server_senddata_size);
	       	 logger.debug("====dt.server.send.protocol:"+dt_server_send_protocol);
	       	 logger.debug("====dt.server.send.address:"+dt_server_send_address);
	       	 
	       	 logger.debug("====dt.server.listen.port:"+dt_server_listen_port);
	       	 logger.debug("====dt.client.readdata.endmark:"+dt_client_readdata_endmark);
            
            isSuccess=propertyVaild();
            if(isSuccess){
            	
            }
            
            return isSuccess;
            
        }  
        catch(Exception e){
        	logger.error(e.toString());
        	return false;
        }
        
    }  
	
	/***
	 * 属性有效性验证
	 * @return
	 */
	private static boolean propertyVaild(){
		boolean isSuccess=true;
		if(!"udp".equalsIgnoreCase(dt_client_read_protocol)&&!"tcp".equalsIgnoreCase(dt_client_read_protocol)){
			isSuccess=false;
			logger.error("error:监听端口协议为 [udp|tcp]");
		}
		
		if(!"udp".equalsIgnoreCase(dt_server_send_protocol)&&!"tcp".equalsIgnoreCase(dt_server_send_protocol)&&!"http".equalsIgnoreCase(dt_server_send_protocol)){
			isSuccess=false;
			logger.error("error:发送服务器端口应为[udp|tcp|http]");
		}
		
		if("http".equalsIgnoreCase(dt_server_send_protocol)){
			
			if("".equals(dt_server_send_address)){
				isSuccess=false;
				logger.error("当设置 dt.server.send.httpurl=http 时 请填写 dt.server.send.httpurl 服务地址");
			}
		}
		
		try {
			Class.forName(dt_client_data_processor);
		} catch (ClassNotFoundException e) {
			isSuccess=false;
			logger.error("error:数据处理机制类初始化失败");
		}
		
		
		
		
		return isSuccess;
		
		
		
	}
}
